import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { PrinterService } from '../../services/admin/printer.service';
import { PrinterTemplateService } from '../../services/admin/printer-template.service';

/**
 * PrinterController
 * 对应 PHP: Printer Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('sys')
@Controller('adminapi/sys')
export class PrinterController {
  constructor(
    private readonly printerService: PrinterService,
    private readonly printerTemplateService: PrinterTemplateService,
  ) {}

  /**
   * 小票打印机
   * 路由: GET printer
   * PHP路由: Route::get('printer', 'sys.Printer/pages')
   */
  @Get('printer')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async pages(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: pages

      return await this.printerService.getPage(data);
    } catch (error) {
      throw new BusinessException('pages操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/list
   * PHP路由: Route::get('printer/list', 'sys.Printer/lists')
   */
  @Get('printer/list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.printerService.getList(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/:id
   * PHP路由: Route::get('printer/:id', 'sys.Printer/info')
   */
  @Get('printer/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async info(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.printerService.getInfo(id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: POST printer
   * PHP路由: Route::post('printer', 'sys.Printer/add')
   */
  @Post('printer')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async add(@Body() data: AddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: add

      return await this.printerService.add(data);
    } catch (error) {
      throw new BusinessException('add操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: PUT printer/:id
   * PHP路由: Route::put('printer/:id', 'sys.Printer/edit')
   */
  @Put('printer/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async edit(
    @Param('id') id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.printerService.edit(id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: PUT printer/status
   * PHP路由: Route::put('printer/status', 'sys.Printer/modifyStatus')
   */
  @Put('printer/status')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async modifyStatus(@Body() data: ModifyStatusDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: modifyStatus

      return await this.printerService.modifyStatus(data);
    } catch (error) {
      throw new BusinessException('modifyStatus操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: DELETE printer/:id
   * PHP路由: Route::delete('printer/:id', 'sys.Printer/del')
   */
  @Delete('printer/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async del(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: del

      return await this.printerService.del(id);
    } catch (error) {
      throw new BusinessException('del操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/template
   * PHP路由: Route::get('printer/template', 'sys.Printer/templatePageLists')
   */
  @Get('printer/template')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async templatePageLists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: templatePageLists

      return await this.printerTemplateService.getPage(data);
    } catch (error) {
      throw new BusinessException('templatePageLists操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/template/list
   * PHP路由: Route::get('printer/template/list', 'sys.Printer/templateLists')
   */
  @Get('printer/template/list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async templateLists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: templateLists

      return await this.printerTemplateService.getList(data);
    } catch (error) {
      throw new BusinessException('templateLists操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/template/:id
   * PHP路由: Route::get('printer/template/:id', 'sys.Printer/templateInfo')
   */
  @Get('printer/template/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async templateInfo(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: templateInfo

      return await this.printerTemplateService.getInfo(id);
    } catch (error) {
      throw new BusinessException('templateInfo操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: POST printer/template
   * PHP路由: Route::post('printer/template', 'sys.Printer/templateAdd')
   */
  @Post('printer/template')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async templateAdd(@Body() data: TemplateAddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: templateAdd

      return await this.printerTemplateService.add(data);
    } catch (error) {
      throw new BusinessException('templateAdd操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: PUT printer/template/:id
   * PHP路由: Route::put('printer/template/:id', 'sys.Printer/templateEdit')
   */
  @Put('printer/template/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async templateEdit(
    @Param('id') id: string,
    @Body() data: TemplateEditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: templateEdit

      return await this.printerTemplateService.edit(id, data);
    } catch (error) {
      throw new BusinessException('templateEdit操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: DELETE printer/template/:id
   * PHP路由: Route::delete('printer/template/:id', 'sys.Printer/templateDel')
   */
  @Delete('printer/template/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async templateDel(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: templateDel

      return await this.printerTemplateService.del(id);
    } catch (error) {
      throw new BusinessException('templateDel操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/type
   * PHP路由: Route::get('printer/type', 'sys.Printer/getType')
   */
  @Get('printer/type')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async getType(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getType

      return await this.printerService.getType();
    } catch (error) {
      throw new BusinessException('getType操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: GET printer/brand
   * PHP路由: Route::get('printer/brand', 'sys.Printer/getBrand')
   */
  @Get('printer/brand')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async getBrand(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getBrand

      return await this.printerService.getBrand(data['brand']);
    } catch (error) {
      throw new BusinessException('getBrand操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: PUT printer/testprint/:id
   * PHP路由: Route::put('printer/testprint/:id', 'sys.Printer/testPrint')
   */
  @Put('printer/testprint/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async testPrint(
    @Param('id') id: string,
    @Body() data: TestPrintDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: testPrint

      return await this.printerService.testPrint(id);
    } catch (error) {
      throw new BusinessException('testPrint操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: PUT printer/refreshtoken/:id
   * PHP路由: Route::put('printer/refreshtoken/:id', 'sys.Printer/refreshToken')
   */
  @Put('printer/refreshtoken/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async refreshToken(
    @Param('id') id: string,
    @Body() data: RefreshTokenDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: refreshToken

      return await this.printerService.refreshToken(id);
    } catch (error) {
      throw new BusinessException('refreshToken操作失败', error);
    }
  }

  /**
   * 小票打印机
   * 路由: POST printer/printticket
   * PHP路由: Route::post('printer/printticket', 'sys.Printer/printTicket')
   */
  @Post('printer/printticket')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '小票打印机' })
  async printTicket(@Body() data: PrintTicketDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: printTicket

      return await this.printerService.printTicket(data);
    } catch (error) {
      throw new BusinessException('printTicket操作失败', error);
    }
  }
}
